#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double db;

#define in read()
#define pii pair<int,int>
#define fi first
#define se second
#define FILE(x) freopen(x".in","r",stdin);\
	freopen(x".out","w",stdout);
#define pb push_back

int read(){
	int x = 0,sgn = 1;char ch = getchar();
	for(;!isdigit(ch);ch = getchar()) if(ch == '-') sgn = -1;
	for(;isdigit(ch);ch = getchar()) x = (x<<1)+(x<<3)+(ch^48);
	return x*sgn;
}

const int N = 1e6+10;

int buf[N];
int *f[N],*now = buf;
int n,h[N],cnt,md[N],ans[N],dep[N],son[N];
struct edge{int v,nxt;}e[N<<1];
void link(int x,int y){e[++cnt] = (edge){y,h[x]};h[x] = cnt;}

void dfs(int x,int p){
	for(int i = h[x],y;i;i = e[i].nxt){
		if((y = e[i].v) == p) continue; dfs(y,x); if(md[y] > md[son[x]]) son[x] = y;
	}md[x] = md[son[x]] + 1;
}

void dfs1(int x,int p){
	f[x][0] = 1;
	if(son[x]){f[son[x]] = f[x] + 1; dfs1(son[x],x);ans[x] = ans[son[x]] + 1;}
	for(int i = h[x],y;i;i = e[i].nxt){
		if((y = e[i].v) == son[x] || y == p) continue;
		f[y] = now; now += md[y]; dfs1(y,x);
		for(int j = 1;j <= md[y];j++){
			f[x][j] += f[y][j-1];
			if(f[x][j] > f[x][ans[x]] || (f[x][j] == f[x][ans[x]] && j < ans[x])) ans[x] = j;
		}
	}
	if(f[x][ans[x]] == 1) ans[x] = 0;
}

int main (){
#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
#endif
	n = in; for(int i = 1,u,v;i < n;i++) u = in,v = in,link(u,v),link(v,u);
	dfs(1,0); f[1] = now; now += md[1]; dfs1(1,0);
	for(int i = 1;i <= n;i++) printf("%d\n",ans[i]);
	return 0;
}

